package com.example.myapplication.part03.part06;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class CalculatorApp extends AppCompatActivity {
    private TextView tvResult;
    private StringBuilder currentNumber = new StringBuilder();
    private double firstNumber = 0;
    private String currentOperation = "";
    private boolean isNewOperation = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_app);
        tvResult = findViewById(R.id.tvResult);
// 数字按钮点击事件
        setNumberButtonClick(R.id.btn0, "0");
        setNumberButtonClick(R.id.btn1, "1");
        setNumberButtonClick(R.id.btn2, "2");
        setNumberButtonClick(R.id.btn3, "3");
        setNumberButtonClick(R.id.btn4, "4");
        setNumberButtonClick(R.id.btn5, "5");
        setNumberButtonClick(R.id.btn6, "6");
        setNumberButtonClick(R.id.btn7, "7");
        setNumberButtonClick(R.id.btn8, "8");
        setNumberButtonClick(R.id.btn9, "9");
        setNumberButtonClick(R.id.btnDot, ".");
// 运算符按钮
        setOperationButtonClick(R.id.btnAdd, "+");
        setOperationButtonClick(R.id.btnSubtract, "-");
        setOperationButtonClick(R.id.btnMultiply, "×");
        setOperationButtonClick(R.id.btnDivide, "÷");
// 清除按钮
        findViewById(R.id.btnClear).setOnClickListener(v -> {
            currentNumber.setLength(0);
            firstNumber = 0;
            currentOperation = "";
            tvResult.setText("0");
            isNewOperation = true;
        });
// 等于按钮
        findViewById(R.id.btnEqual).setOnClickListener(v -> calculateResult());
    }
    private void setNumberButtonClick(int buttonId, String number) {
        findViewById(buttonId).setOnClickListener(v -> {
            if (isNewOperation) {
                currentNumber.setLength(0);
                isNewOperation = false;
            }
// 防止多个小数点
            if (number.equals(".") && currentNumber.toString().contains(".")) {
                return;
            }
            currentNumber.append(number);
            tvResult.setText(currentNumber.toString());
        });
    }
    private void setOperationButtonClick(int buttonId, String operation) {
        findViewById(buttonId).setOnClickListener(v -> {
            if (currentNumber.length() > 0) {
                firstNumber = Double.parseDouble(currentNumber.toString());
                currentOperation = operation;
                currentNumber.setLength(0);
                isNewOperation = true;
            }
        });
    }
    private void calculateResult() {
        if (currentNumber.length() > 0 && !currentOperation.isEmpty()) {
            double secondNumber = Double.parseDouble(currentNumber.toString());
            double result = 0;
            switch (currentOperation) {
                case "+":
                    result = firstNumber + secondNumber;
                    break;
                case "-":
                    result = firstNumber - secondNumber;
                    break;
                case "×":
                    result = firstNumber * secondNumber;
                    break;
                case "÷":
                    if (secondNumber != 0) {
                        result = firstNumber / secondNumber;
                    } else {
                        tvResult.setText("Error");
                        return;
                    }
                    break;
            }
// 处理整数显示
            if (result == (int) result) {
                tvResult.setText(String.valueOf((int) result));
            } else {
                tvResult.setText(String.valueOf(result));
            }
            currentNumber.setLength(0);
            currentNumber.append(result);
            isNewOperation = true;
            currentOperation = "";
        }
    }
}